package com.allwees.bs.c.module.app.controller;

import com.allwees.bs.c.configuration.CountryConfig;
import com.allwees.bs.c.module.app.dto.AppInitDTO;
import com.allwees.bs.c.module.app.service.IAppService;
import com.allwees.bs.c.module.app.vo.AppInitVO;
import com.allwees.bs.c.module.app.vo.UserInfoCollectVo;
import com.allwees.bs.c.module.base.controller.BaseController;
import com.allwees.bs.c.module.product.service.CategoryService;
import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.core.common.util.http.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * APP控制相关
 */
@RestController
public class AppController extends BaseController {

    @Autowired
    private IAppService appService;

    @Autowired
    private CategoryService categoryService;

    @Resource
    private CountryConfig countryConfig;

    /**
     * APP初始化所需的信息
     *
     * @param initDTO
     * @return
     */
    @PostMapping("/api/v2/app/init")
    public R appInitV2(@RequestBody AppInitDTO initDTO) {
        return R.ok(new AppInitVO(categoryService.getAllCategories()));
    }

    @GetMapping("/api/v2/app/init")
    public R appInitV2() {
        return R.ok(new AppInitVO(categoryService.getAllCategories()));
    }

    @GetMapping("/api/v1/countries")
    public R countries() {
        Map<String, Object> map = new HashMap<>();
        map.put("countries", countryConfig.getCountries());
        map.put("imgs", countryConfig.getImgs());
        return R.ok(map);
    }

    /**
     * 收集用户信息
     *
     * @param userInfoCollectVo
     * @return
     */
    @Deprecated
    @PostMapping("/api/v1/app/info/collect")
    public R infoInit(@RequestBody @Validated UserInfoCollectVo userInfoCollectVo) {
        appService.setUserInfoCollect(getUserUuid(), userInfoCollectVo);
        return R.ok();
    }

    /**
     * 获取平台banner
     *
     * @param platform
     * @param bannerType
     * @return
     */
    @Deprecated
    @GetMapping("/api/v2/app/banner/{platform}/{bannerType}")
    public R banners(@PathVariable String platform, @PathVariable String bannerType) {
        return R.ok();
    }

    /**
     * 心跳检测
     *
     * @return
     */
    @GetMapping("/api/v1/app/heartbeat")
    public R heartbeat() {
        return R.ok("I'm alive!");
    }

    /**
     * 检查App版本号
     *
     * @return
     */
    @GetMapping("/api/v2/app/check")
    public R checkAppVersion(HttpServletRequest request) {
        String channel = RequestContext.getChannel();
        return R.ok(appService.checkAppVersion(channel));
    }
}
